package cn.com.homeyoda.aimate.aihealth.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;

import cn.com.homeyoda.aimate.aihealth.R;


/**
 * Created By 刘纯贵
 * Created Time 2020/6/3
 */
public class ShowLoadingUtil {
    private static Context mContext;
    private static Dialog dialog;
    private static View inflate;
    private static LottieAnimationView lottieAnimationView;
    private static TextView textView;

    public static void showLoading(Context context) {
        Log.i("huadi","showLoading====dialog========"+dialog);
        showLoading(context, "加载中...");
    }

    public static void showLoading(Context context,String message){
        //mContext = context;
        if (context == null || (context instanceof Activity && ((Activity) context).isFinishing())) {
            return;
        }
        // 2. 清除旧 Dialog（防止内存泄漏）
        if (dialog != null && dialog.isShowing()) {
            Log.i("dahua","dialog====dismiss========");
            dialog.dismiss();
            dialog = null;
        }


            dialog = new Dialog(context, R.style.MyDialog);
            inflate = LayoutInflater.from(context).inflate(R.layout.layout_loading, null);
            dialog.setContentView(inflate);
            dialog.setCanceledOnTouchOutside(false);
            lottieAnimationView = inflate.findViewById(R.id.animation_view);
            lottieAnimationView.setAnimation("loading.json");
            lottieAnimationView.loop(true);
            // 设置循环
            lottieAnimationView.setRepeatCount(LottieDrawable.INFINITE);
            // 播放动画
            lottieAnimationView.playAnimation();
            textView = dialog.findViewById(R.id.text);
            textView.setText(message);
//        loadingView = inflate.findViewById(R.id.loadView);
//        loadingView.setLoadingText("加载中...");
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity( Gravity.CENTER_VERTICAL);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = dpToPx(150,context);  // 160dp
            lp.height = dpToPx(150,context); // 160dp
            lp.y = 20;
            dialogWindow.setAttributes(lp);




        if (!dialog.isShowing()) {
            dialog.show();
        }
        Log.i("huadi","dialog.==show=="+dialog);
        new Handler().postDelayed(() -> dismiss(), 1500);
    }



    private static int dpToPx(int dp,Context context) {
        return (int) (dp * context.getResources().getDisplayMetrics().density);
    }

    public void Show(){
        dialog.show();
    }

    public static void dismiss(){
        Log.i("dahua","dialog.==dismiss=="+dialog);
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
